package test;

import lib.db_properties_path.*;
import java.sql.*;
import java.util.*;
import lib.dbTool.*;

public class SCORE extends DBconnection2 {
	public SCORE(SetPath set) {
		super(set);
	}

	public SCORE() {
		super(new DefaultSetPath());
	}

	/** Auto Increasment */
	/** Primary Key */
	/** INT ; java.lang.Integer */
	private int idscore;
	/** INT ; java.lang.Integer */
	private int idstudent;
	/** VARCHAR ; java.lang.String */
	private String classname;
	/** INT ; java.lang.Integer */
	private int score;

	public int getIdscore() {
		return idscore;
	}

	public void setIdscore(int idscore) {
		this.idscore = idscore;
	}

	public int getIdstudent() {
		return idstudent;
	}

	public void setIdstudent(int idstudent) {
		this.idstudent = idstudent;
	}

	public String getClassname() {
		return classname;
	}

	public void setClassname(String classname) {
		this.classname = classname;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public interface fn {
		String TABLE_NAME = "SCORE";
		String PK = "idscore";
		String idscore = "idscore";
		String idstudent = "idstudent";
		String classname = "classname";
		String score = "score";
	}

	public String sql(String SQL) {
		return Utility.getSQL組合字(SQL, SCORE.fn.class);
	}

	public int clear() {
		String SQL = "delete from SCORE";
		Object parameter[] = {};
		return super.excuteUpdate(SQL, parameter);
	}

	public int delete(int idscore) {
		String SQL = "delete from SCORE where idscore=?";
		Object parameter[] = { idscore };
		return super.excuteUpdate(SQL, parameter);
	}

	public int update(int idstudent, String classname, int score, int idscore) {
		String SQL = "update SCORE set idstudent=?,classname=?,score=? where idscore=?";
		Object parameter[] = { idstudent, classname, score, idscore };
		return super.excuteUpdate(SQL, parameter);
	}

	public int insert(int idstudent, String classname, int score) {
		String SQL = "insert into SCORE(idstudent,classname,score) values(?,?,?)";
		Object parameter[] = { idstudent, classname, score };
		return super.excuteUpdate(SQL, parameter);
	}

	public String[][] select() {
		String SQL = "select * from SCORE";
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public SCORE[] select_Object() {
		String SQL = "select * from SCORE";
		Object parameter[] = {};
		String data[][] = super.select(SQL, parameter);
		SCORE obj[] = toObject(data);
		return obj;
	}

	public String[][] select(int idscore) {
		String SQL = "select * from SCORE where idscore=?";
		Object parameter[] = { idscore };
		return super.select(SQL, parameter);
	}

	public SCORE[] select_Object(int idscore) {
		String data[][] = this.select(idscore);
		SCORE obj[] = toObject(data);
		return obj;
	}

	public int selectCount() {
		String SQL = "select count(idscore) from SCORE";
		Object parameter[] = {};
		String d[][] = super.select(SQL, parameter);
		return Integer.parseInt(d[0][0]);
	}

	public String[][] selectPage(int pageSize, int pageIndex, int rowCount) {
		int desc = PaggingSQL.ASC;
		PaggingSQL ps = new lib.dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPage(pageSize, pageIndex, TABLE_NAME, PK,
				desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount) {
		PaggingSQL ps = new lib.dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	/**
	 * 關鍵字 where 不用寫
	 * 
	 * @param pageSize
	 * @param pageIndex
	 * @param sortName
	 * @param desc
	 * @param rowCount
	 * @param whereCondition
	 *            = 【pk>0】, 關鍵字 where 不用寫
	 * @return
	 */
	public String[][] selectPageOrderBy(int pageSize, int pageIndex,
			String sortName, int desc, int rowCount, String whereCondition) {
		PaggingSQL ps = new lib.dbTool.MySQL();
		String TABLE_NAME = fn.TABLE_NAME;
		String PK = fn.PK;
		// 範例,關鍵字 where 不用寫, ex:【pk>0】
		String SQL = ps.getSelectPageOrderBy(pageSize, pageIndex, TABLE_NAME,
				PK, sortName, desc, rowCount, whereCondition);
		Object parameter[] = {};
		return super.select(SQL, parameter);
	}

	public SCORE[] selectPage_Object(int pageSize, int pageIndex, int rowCount) {
		String data[][] = this.selectPage(pageSize, pageIndex, rowCount);
		SCORE obj[] = toObject(data);
		return obj;
	}

	/**
	 * 分組統計筆數
	 * 
	 * @param fk_List
	 * @return
	 */
	public Map<Integer, Integer> getGroupCount(String fkName,
			LinkedList<Integer> fk_List) {
		String q = "";
		Object parameter[] = new Object[fk_List.size()];
		int i = 0;
		for (Integer no : fk_List) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		String SQL = "";
		SQL += "SELECT " + fkName + ", count(" + fkName + ") ";
		SQL += "FROM " + fn.TABLE_NAME + " where " + fkName + " in (" + q + ")";
		SQL += "group by " + fkName + ";";
		String data[][] = super.select(SQL, parameter);
		for (int j = 0; j < data.length; j++) {
			int key = Integer.parseInt(data[j][0]);
			int count = Integer.parseInt(data[j][1]);
			map.put(key, count);
		}
		return map;
	}

	/**
	 * Where PK in(?,?,?....)
	 * 
	 * @param idList
	 * @return
	 */
	public Map<Integer, SCORE> selectMap(LinkedList<Integer> idList) {
		String q = "";
		Object parameter[] = new Object[idList.size()];
		int i = 0;
		for (Integer no : idList) {
			q += "?,";
			parameter[i++] = no;
		}
		if (q.length() > 0) {
			q = q.substring(0, q.length() - 1);
		}
		HashMap<Integer, SCORE> map = new HashMap<Integer, SCORE>();

		String SQL = "select * from SCORE where idscore in (" + q + ")";
		String data[][] = super.select(SQL, parameter);
		SCORE[] obj = this.toObject(data);
		for (SCORE o : obj) {
			map.put(new Integer(o.idscore), o);
		}
		return map;
	}

	public SCORE[] toObject(String data[][]) {
		SCORE[] obj = new SCORE[data.length];
		for (int i = 0; i < obj.length; i++) {
			obj[i] = new SCORE(super.getSetting());
			obj[i].idscore = Integer.parseInt(data[i][0]);
			obj[i].idstudent = Integer.parseInt(data[i][1]);
			obj[i].classname = data[i][2];
			obj[i].score = Integer.parseInt(data[i][3]);
		}
		return obj;
	}
}